package com.zhijian.demo.service;

import com.zhijian.demo.mapper.UserMapper;
import com.zhijian.demo.pojo.Res;
import com.zhijian.demo.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * TODO
 *
 * @author chenzhijian
 * @date 2023/10/17
 */
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public Res addUser(User user) {
        int i = userMapper.addUser(user);
        if(i != 0) return new Res(0, "添加" + i + "个用户成功");
        else return new Res(1, "添加用户失败！");
    }

    public Res deleteUserByUserName(String username){
        int i = userMapper.deleteUserByUserName(username);
        if(i != 0) return new Res(0, "删除" + i + "个用户成功");
        else return new Res(1, "删除用户失败！");
    }

    public Res updateUserByUsername(String oldUsername, User newUser) {
        int i = userMapper.updateUserByUsername(oldUsername, newUser);
        if(i != 0) return new Res(0, "修改用户" + oldUsername + "信息成功");
        else return new Res(1, "修改用户信息失败！");
    }

    public Res selectUsers() {
        List<User> users = userMapper.selectUsers();
        if(users.size() != 0) return new Res(0, users.toString());
        else return new Res(1, "查询所有用户失败");
    }
}
